<!--
 * @Descripttion:
 * @Author: zhengminjie,xiayujia
 * @Date: 2021-04-05 13:23:43
 * @LastEditors: zhengminjie
 * @LastEditTime: 2021-04-05 16:08:14
-->
<template>
  <div>
    <el-dropdown @command="handleCommand">
      <span class="el-dropdown-link">
        <el-badge class="mark;margin-top: 10px;margin-right: 40px" :value='allMessages'> 
          <el-button icon="el-icon-message" circle></el-button>
        </el-badge>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item class="clearfix" command="a" ref="tb">
          有新成员加入
          <el-badge class="mark" :value="joinMessages" />
        </el-dropdown-item>
        <el-dropdown-item class="clearfix" command="b" ref="tb">
          你有一份新文件或新表格
          <el-badge class="mark" :value="docMessages" />
        </el-dropdown-item>
        <el-dropdown-item class="clearfix" command="c" ref="tb">
          你有一条入群消息
          <el-badge class="mark" :value="joinGroupMessages" />
        </el-dropdown-item>
        <el-dropdown-item class="clearfix" command="d" ref="tb">
          你有一条被删或被拒绝消息
          <el-badge class="mark" :value="deleteMessages" />
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>

    <!-- 表格文档 对话框 -->
    <el-dialog title="你有一份新文件或新表格" :visible.sync="detailopen" append-to-body>
      <!--
      <div>
        <el-table v-loading="loading" :data="dataDetail" @selection-change="handleSelectionChange" >
          <el-table-column prop="content" label="这里会显示主要内容" align="center" :show-overflow-tooltip="true"/>
          <el-table-column label="操作" align="center" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-button 
                type="primary" 
                plain
                size="mini"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:data:edit']"
                >编辑</el-button>
              </template>
            </el-table-column>
        </el-table>
     </div>
     -->
   </el-dialog>

   <!-- 群有人加入消息 对话框 -->
   <el-dialog title="你有一条入群消息" :visible.sync="joinOpen" append-to-body center width="400px">
      <el-form ref="joinForm" :model="joinForm" label-width="100px">
        <el-form-item label="群号">
          <el-input v-model="joinForm.groupNum" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="joinForm.userName" disabled></el-input>
        </el-form-item>
        <el-form-item label="为什么入群">
          <el-input v-model="joinForm.content" disabled></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">同意</el-button>
          <el-button @click="reject">拒绝</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>

import {countMessages, joinMessages, addMessages, getMessages, refuseMessages, getdoc, agree, disagree} from '@/api/distribution/messages'

export default {
  data(){
    return{
      // 数据具体信息弹窗打开
      detailopen: false,
      // 入群对话框
      joinOpen:false,  
      // 总消息数
      allMessages:0,
      // 新成员加入
      joinMessages:0,
      // 新文件
      docMessages:0,
      // 入群消息
      joinGroupMessages:0,
      // 被删消息
      deleteMessages:0,
      // 入群数据
      joinForm:{},
      // 定时器
      timer:"",
    }
  },
  created(){
    // 设置定时器 每个五分钟刷新一下消息数
    this.timer = setInterval(this.getMessage(),300000);
  },
  methods:{
    // 得到消息数
    getMessage(){
      this.allMessages = 0;
      countMessages(0).then(res=>{
        this.docMessages = res;
        this.allMessages += res;
      })
      countMessages(1).then(res=>{
        this.joinMessages = res;
        this.allMessages += res;
      })
      countMessages(2).then(res=>{
        this.joinGroupMessages = res;
        this.allMessages += res;
      })
      countMessages(3).then(res=>{
        this.deleteMessages = res;
        this.allMessages += res;
      })
    },
    // 点击某个消息
    handleCommand(command){
      if(command == "a"){
        if(this.joinMessages != 0 ){
          joinMessages(1).then(res=>{
            let content = res.split(",");
            this.joinForm.groupNum = content[0];
            this.joinForm.userName = content[1];
            this.joinForm.content = content[2];
            this.joinForm.id = content[3];
            this.joinOpen = true;
          })
        }
      }
      else if(command == "b"){
        if(this.docMessages != 0){
          getdoc().then(res=>{
            if(res.length!=""){
              this.$confirm(res[0], '你有一份新文件或新表格', {
                distinguishCancelAndClose: true,
                confirmButtonText: '同意接收',
                cancelButtonText: '拒绝接收',
              }).then(() => {
                this.Agree();
              }).catch(action => {
                if(action == 'cancel'){
                  this.Disagree();
                }
                else{
                  this.$message({
                    type: 'info',
                    message: '已取消'
                  })    
                } 
              });
            }
          })
        }
      }
      else if(command == "c"){
        if(this.joinGroupMessages != 0){
          getMessages(2).then(res=>{
            this.$message({
              message: res,
              type:'success'
            });
            this.joinGroupMessages--;
            this.allMessages--;
          })
        }
      }
      else{
        if(this.deleteMessages != 0){
          getMessages(3).then(res=>{
            this.$message({
              message: res,
              type:'success'
            });
            this.deleteMessages--;
            this.allMessages--;
          })
        }
      }
    },
    onSubmit(){
      let add = {};
      add.id = this.joinForm.id;
      add.userName = this.joinForm.userName;
      add.messageType = 2;
      add.messageContent = "你有一条入群消息";
      add.content = this.joinForm.groupNum;
      addMessages(add).then(res=>{
        console.log(res);
        this.joinMessages--;
        this.allMessages--;
        this.joinOpen = false;
      })
    },
    reject(){
      let refuse = {};
      refuse.id = this.joinForm.id;
      refuse.userName = this.joinForm.userName;
      refuse.messageType = 3;
      refuse.messageContent = "你有一条被拒绝消息";
      refuse.content = this.joinForm.groupNum;
      refuseMessages(refuse).then(res=>{
        console.log(res);
        this.joinMessages--;
        this.allMessages--;
        this.joinOpen = false;
      })
    },
    Agree(){
      agree().then(val=>{
        this.$message({
          type: 'success',
          message: '同意接收!'
        });
        this.docMessages--;
        this.allMessages--;
      })
    },
    Disagree(){
      disagree().then(val=>{
        this.$message({
          type: 'info',
          message: '已拒绝接收'
        });    
        this.docMessages--;
        this.allMessages--;
      })
    }
  }
}
</script>